// pages/stock/detailShare.js
var app = getApp()
Page({
  data: {
    agentId:'',
    accountId:'',
    goodsAttr:[],
    currentItem:'',
    currentIndex:'',
    currentBox:[],
    isStock:false,
    enterCart:true,
    //商品信息
    id:'',name:'',imgUrl:'',price:'',sales:'',stock:'',productId:'',goodsType:'',thumbUrl:'',product_type:'',applyTo:'',versionValues:'',versionNameId:'',
    //厂家信息
    brand_url:'',brand_desc:'',brand_name:'',brand:true,
    //配送信息
    logistics:'',
    //商品详情
    details:[],
    num:1,
    actPrice:'',
    totalPrice:'',
    versionValueIds:'',
    ver_value_ids:'',
    typeDetailList:'',
    product_code:'',
    color:'',
    companyName:'',
    searchType:'',
    productType:''
  },

  onLoad: function (options) {
    let mark = options.mark
    let goodsType = options.goodsType, //一级分类
    productType = options.product_type,//二级分类
    goodsId = options.id,
    mtId = options.mtId,
    applyTo = options.applyTo,
    goodsPrice = options.price=="null"?0:options.price
    this.setData({
      price:goodsPrice,
      totalPrice:goodsPrice,
      productId:goodsId,
      goodsType,
      productType,
      applyTo,
      mtId,
      color:options.color,
      companyName:options.companyName
    })
    app.getMemInfo().then(res=>{
      let agentId = res.data.agentId
      let accountId = res.data.id
      if (mark==1) {
        this.getGoodsDetail2(agentId,goodsType,goodsId)
      }else{
        this.getGoodsDetail(agentId,goodsType,goodsId)
      }
      this.setData({
        agentId,
        accountId
      })
    })
    if (options.type==1) {
      this.setData({
        isStock:true
      })
    }
  },


  onShareAppMessage: function () {

  },
  //获得商品详情
  getGoodsDetail(agentId,type,skuId){
    // wx.showLoading({
    //   title:'正在加载'
    // })
    var data = {
      agentId,
      type,
      skuId
    }
    console.log(data)
    var that = this
    app.request('wx_app/mall/product/detail',data,'GET').then(res=>{
      console.log(res)
      if (res.code==1) {
        let details = []
        details.push(
          res.data.sku.imgUrl2,
          res.data.sku.imgUrl3,
          res.data.sku.imgUrl4,
          res.data.sku.imgUrl5,)
        that.setData({
          id:res.data.sku.id,
          name:res.data.sku.productName,
          imgUrl:res.data.sku.mainImgUrl,
          sales:res.data.sku.sales,
          stock:res.data.sku.stock,
          brand_url:res.data.brand_url?res.data.brand_url:'',
          brand_desc:res.data.brand_desc?res.data.brand_desc:'',
          brand_name:res.data.brand_name?res.data.brand_name:'',
          applyTo:res.data.sku.applyTo,
          versionValues:res.data.sku.versionValues,
          versionNameId:res.data.sku.versionNameId,
          details,
          product_type:res.data.sku.versionValues+" "+res.data.sku.colorName,
          searchType:res.data.sku.productType
      })
        if (!res.data.brand_name) {
          that.setData({
            brand:false
          })
        }else{
          that.setData({
            brand:true
          })
        }
        wx.hideLoading()
      }else{
        wx.hideLoading()
        app.errorMsg(res.message)
      }
    })
  },
  getGoodsDetail2(agentId,type,productId){
    wx.showLoading({
      title:'正在加载'
    })
    var data = {
      agentId,
      type,
      productId
    }
    console.log(data)
    var that = this
    app.request('wx_app/mall/product/order_detail',data,'GET').then(res=>{
      console.log(res)
      if (res.code==1) {
        let details = []
        res.data.details.forEach(item=>{
          details.push(item.url)
        })
        that.setData({
          name:res.data.product_name,
          imgUrl:res.data.url,
          sales:res.data.sales,
          stock:res.data.stock,
          brand_url:res.data.brand_url?res.data.brand_url:'',
          brand_desc:res.data.brand_desc?res.data.brand_desc:'',
          brand_name:res.data.brand_name?res.data.brand_name:'',
          typeDetailList:res.data.apply_to==null?false:res.data.apply_to,
        // logistics:res.data.logistics,
          product_code:res.data.product_code?res.data.product_code:'暂无',
          details
      })
        if (!res.data.brand_name) {
          that.setData({
            brand:false
          })
        }else{
          that.setData({
            brand:true
          })
        }
        wx.hideLoading()
      }else{
        wx.hideLoading()
        app.errorMsg(res.message)
      }
    })
  },
})
